package com.yiduo.outpost.gateway.config;

import com.alibaba.cloud.sentinel.gateway.ConfigConstants;
import com.alibaba.cloud.sentinel.gateway.scg.SentinelGatewayProperties;
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;


/**
 * @Author yanwen
 * @create 2023/2/14 9:24 PM
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(GlobalFilter.class)
@ConditionalOnProperty(prefix = ConfigConstants.GATEWAY_PREFIX, name = "enabled",
    havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(SentinelGatewayProperties.class)
@AutoConfigureBefore(value = com.alibaba.cloud.sentinel.gateway.scg.SentinelSCGAutoConfiguration.class)
public class SentinelGatewayConfiguration {
    @Bean
    @Order(-1)
    @ConditionalOnMissingBean
    public SentinelGatewayFilter sentinelGatewayFilter() {
        log.info(
            "[Sentinel SpringCloudGateway] register SentinelGatewayFilter with order: {}",
            Ordered.HIGHEST_PRECEDENCE);
        return new SentinelGatewayFilter(Ordered.HIGHEST_PRECEDENCE,new ServerWebExchangeItemParser());
    }
}
